*** Generating figure 3: main results split by subsamples ***

** Preamble **

clear all
set more off


local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/figures"

* Note: local directory is the output folder 
cd "`path_output'"

** Loading the input file **
use `path_input'/dataset_final_may20, clear


xtset codigo_ibge turno

cap drop sh_Tesquerda
cap drop sh_Tdireita
gen sh_Tesquerda=sh_Tpt_+sh_Tpdt_+sh_Tpsdb_+sh_Tpcb_+sh_Tpv_
gen sh_Tdireita=1-sh_Tesquerda-sh_Tnulos_-sh_Tbrancos_

foreach var in sh_radio1991 sh_rural1991{
gen T1`var'=`var' if turno==2
replace T1`var'=0 if T1`var'==.
}

replace pop1991=pop1991 /1000

xtreg sh_Tpt_ treatment sh_Tpsp_-sh_Tpsdb_  i.turno, vce(cluster codigo_ibge) fe
keep if e(sample)==1


/*generating pscores*/
encode uf, gen(UF)
psmatch2 Globo T1* if turno==1 ,  noreplace neighbor(1)
egen ppp=mean(_pscore), by(codigo)
gen pscore=1
replace pscore=ppp/(1-ppp) if Globo==0

foreach var in pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural1991 sh_radio1991{
sum `var'
gen T_`var'=treatment*(`var'-r(mean))
}


/*generating Figure*/
twoway (kdensity ppp if Globo==1) || (kdensity ppp if Globo==0),			 /// 
	   ylab(, nogrid) ytitle("") xlab(, nogrid) xtitle("") 					 /// 
	   graphregion(color(white)) bgcolor(white) legend(off)
